/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package savet.gui.tabelas.group;

import java.util.SortedMap;
import java.util.TreeMap;
import javax.swing.JTable;
import javax.swing.table.JTableHeader;
import javax.swing.table.TableColumnModel;
import javax.swing.table.TableModel;

/**
 *
 * @author Rafael
 */
public class JTableGroupableHeader extends JTable {

    private SortedMap<String, ColumnGroup> mapColumnGroup = new TreeMap<String, ColumnGroup>();

    @Override
    protected JTableHeader createDefaultTableHeader() {
        GroupableTableHeader  header= new GroupableTableHeader(columnModel);
        return header;
    }

    public void addColumnGroup(String keyHeaderGroup) {
        if (mapColumnGroup.get(keyHeaderGroup) == null) {
            mapColumnGroup.put(keyHeaderGroup, new ColumnGroup(keyHeaderGroup));
        }
    }

    public void groupColumn(String keyHeaderGroupMestre, String keyHeaderGroup) {
        addColumnGroup(keyHeaderGroupMestre);
        addColumnGroup(keyHeaderGroup);
        ColumnGroup g_nameMestre = mapColumnGroup.get(keyHeaderGroupMestre);
        g_nameMestre.add(mapColumnGroup.get(keyHeaderGroup));
        GroupableTableHeader header = (GroupableTableHeader) this.getTableHeader();
        header.addColumnGroup(g_nameMestre);
    }

    public void groupColumn(String keyHeaderGroup, int indice) {
        addColumnGroup(keyHeaderGroup);
        TableColumnModel cm = this.getColumnModel();
        ColumnGroup g_name = mapColumnGroup.get(keyHeaderGroup);
        g_name.add(cm.getColumn(indice));
        GroupableTableHeader header = (GroupableTableHeader) this.getTableHeader();
        header.addColumnGroup(g_name);
    }

    public void groupColumns(String keyHeaderGroup, int indiceInicio, int indiceFim) {
        addColumnGroup(keyHeaderGroup);
        TableColumnModel cm = this.getColumnModel();
        ColumnGroup g_name = new ColumnGroup(keyHeaderGroup);
        for (int i = indiceInicio; i <= indiceFim; i++) {
            g_name.add(cm.getColumn(i));
        }
        GroupableTableHeader header = (GroupableTableHeader) this.getTableHeader();
        header.addColumnGroup(g_name);
    }

    public void groupColumns(String keyHeaderGroup, int indiceFim) {
        addColumnGroup(keyHeaderGroup);
        TableColumnModel cm = this.getColumnModel();
        ColumnGroup g_name = new ColumnGroup(keyHeaderGroup);
        for (int i = 0; i <= indiceFim; i++) {
            g_name.add(cm.getColumn(i));
        }
        GroupableTableHeader header = (GroupableTableHeader) this.getTableHeader();
        header.addColumnGroup(g_name);
    }

    public SortedMap<String, ColumnGroup> getMapColumnGroup() {
        return mapColumnGroup;
    }

    @Override
    public void setModel(TableModel dataModel) {
        if (mapColumnGroup != null)
            mapColumnGroup.clear();
        super.setModel(dataModel);
    }

}
